package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.home.adapter.HomeCollectionAdapter;
import com.meitianhui.convenitenbuy.module.home.contract.HomeCollectionContract;
import com.meitianhui.convenitenbuy.module.home.di.DaggerHomeComponent;
import com.meitianhui.convenitenbuy.module.home.di.HomeModule;
import com.meitianhui.convenitenbuy.module.home.presenter.HomeCollectionPresenter;
import com.meitianhui.convenitenbuy.module.mine.model.CollectedStoresBean;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.CollectStoreEventBean;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/28.
 */

public class HomeCollectionActivity extends BaseToolbarActivity implements HomeCollectionContract.View {

    private RecyclerView mRvHomeCollection;
    private String dataSource;
    private String mGoodsCode;      // 商品码
    private String mGoodsUrl;       // 淘口令
    private String mTaoBaoLink;     // 淘宝链接

    private HomeCollectionAdapter mAdapter;
    private View mNotDataView;
    private List<CollectedStoresBean.Stores> collectedStoresList;
    private SettingStoreDialog mSettingStoreDialog;
    @Inject
    HomeCollectionPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_home_collection;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerHomeComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
        setTbTitle("我的收藏");
        mRvHomeCollection = getViewById(R.id.rv_home_collection);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvHomeCollection.setLayoutManager(layoutManager);
        mNotDataView = getLayoutInflater().inflate(R.layout.data_empty_goods, (ViewGroup) mRvHomeCollection.getParent(), false);
        initData();
    }

    private void initData() {
        Intent intent = this.getIntent();
        dataSource = intent.getStringExtra("data_source");
        mGoodsCode = intent.getStringExtra("goods_code");
        mGoodsUrl = intent.getStringExtra("goods_url");
        mTaoBaoLink = intent.getStringExtra("taobao_link");
        mAdapter = new HomeCollectionAdapter(null);
        mRvHomeCollection.setAdapter(mAdapter);
        presenter.getCollection();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CollectedStoresBean.Stores store = collectedStoresList.get(position);

                mSettingStoreDialog = new SettingStoreDialog(HomeCollectionActivity.this,
                        R.style.dialog_with_alpha, new SettingStoreDialog.AlertDialogListener() {
                    @Override
                    public void onConfirmClick() {
                        mSettingStoreDialog.dismiss();
                        presenter.officialStorePost(store.stores_id, store.stores_name, mGoodsCode, mGoodsUrl);
                        presenter.settingDefaultStore(store.stores_id);
                    }

                    @Override
                    public void onCancelClick() {
                        mSettingStoreDialog.dismiss();
                        presenter.officialStorePost(store.stores_id, store.stores_name, mGoodsCode, mGoodsUrl);
                    }
                });
                mSettingStoreDialog.show();
                mSettingStoreDialog.showCancelBtn(true);
                mSettingStoreDialog.setDialogTitle("提示");
                mSettingStoreDialog.setDialogContent("是否将此门店设为默认门店");
            }
        });

    }

    @Override
    public void getCollectionSuccess(CollectedStoresBean data) {
        refreshFinish();
        final int size = data.list == null ? 0 : data.list.size();
        mAdapter.setNewData(data.list);
        if (size == 0) {
            mAdapter.setEmptyView(mNotDataView);
        }
        collectedStoresList = mAdapter.getData();
    }

    @Override
    public void officialStorePostSuccess(EmptyInfo data) {
        mSettingStoreDialog.dismiss();
        TipUtils.showToast(App.getInstance(), "领取成功", 1);
        if (!TextUtils.isEmpty(mTaoBaoLink)) {
            SystemUtils.open(HomeCollectionActivity.this, mTaoBaoLink, data.order_id);
        } else {
            Intent intent = new Intent(HomeCollectionActivity.this, HomeOrderActivity.class);
            startActivity(intent);
        }
        this.finish();
    }

    @Override
    public void officialStorePostFail(int code, String msg) {
        TipUtils.showToast(App.getInstance(), msg);
        this.finish();
    }

    @Override
    public void settingDefaultStoreSuccess() {
        RxBus.get().post(new CollectStoreEventBean(Constants.COLLECT_MENU_COMFIRM));
    }

    @Override
    protected void onRefreshData() {
        presenter.getCollection();
    }

    @Override
    public void showError(String msg) {
    }

}
